Private Sub wsDCMMain_DataArrival(Index As Integer, ByVal bytesTotal As Long)

        On Error Resume Next
        
        Dim lngError As Long
        Dim intX As Integer
        Dim lngX As Long
        Dim strRCV As String
        Dim blnDCMEvent As Boolean
        Dim intDCMEvent As Integer
        Dim blnFound As Boolean
        Dim strOut As String
        Dim strAlarm As String
        Dim intUnit As Integer
        Dim byteMajor As Byte
        Dim byteMinor As Byte
        Dim byteFlag As Byte
        Dim byteType As Byte
        Dim dblSize As Double
        Dim dblSequenceLength As Double
        Dim strRequestID As String
        Dim dblReplyStatus As Double
        Dim strStubData As String
        Dim strAltSrc() As String
        Dim lngAltSrc As Long
        Dim lngStart As Long
        Dim dblX As Double
        
        'Set Unit
        intUnit = 1
        
        'Get Data
        wsDCMMain(Index).GetData strRCV
        
        'Log Data Received
        Call DCM_LOG(Index, intUnit, strRCV, False)
        
               
        'Confirm GIOP Magic Number
        If Mid$(strRCV, 1, 4) = "GIOP" Then
            'Good Data
            'General Inter-ORB Protocol Header
            'Get Major Version
            byteMajor = Asc(Mid(strRCV, 5, 1))
            byteMinor = Asc(Mid(strRCV, 6, 1))
            byteFlag = Asc(Mid(strRCV, 7, 1))
            byteType = Asc(Mid(strRCV, 8, 1))
            'dblSize = Mid(strRcv, 9, 4)
        
            
            'Confirm Version
            If byteMajor = 1 Then
                If byteMinor = 1 Then
                    'Get General Inter-ORB Protocol Reply
                    'dblSequenceLength = Mid(strRcv, 13, 4)
                    strRequestID = Mid(strRCV, 17, 4)
                    'dblReplyStatus = Mid(strRcv, 21, 4)   '65536
                    strStubData = Mid(strRCV, 25)
                    
                    'Check For Error
                    lngError = InStr(1, strRCV, "DCM/OpNotSucceeded", vbBinaryCompare)
100                 If lngError > 0 Then
                        'Failed
                        'Extract Event #
                        intDCMEvent = Asc(Mid$(strRequestID, 4, 1))
                        
                        'Increment Retry
                        typDCM_TX(intDCMEvent).intMainRetry = typDCM_TX(intDCMEvent).intMainRetry + 1
                        
                        'Lock It So Can't Send Again On This Connection Session
                        typDCM_TX(intDCMEvent).intMainRetryLock = 10
                        
                        'Logit
                        Call DCM_LOG(Index, intUnit, "DCM " + strSplicerCategoryNames(1) + " retry #" + CStr(typDCM_TX(intDCMEvent).intMainRetry), True, True)
                        
                        'Check Retry Count
                        If typDCM_TX(intDCMEvent).intMainRetry >= 3 Then
                             'Done Trying
                             'Build Alarm
                             strAlarm = "A DCM error has occured. "
                             If lngError = 1000 Then
                                 strAlarm = strAlarm + "  Input IPS_FindTSs Error."
                             End If
                             If lngError = 2000 Then
                                 strAlarm = strAlarm + "  Output IPS_FindTSs Error."
                             End If
                             strAlarm = strAlarm + Client_Build_Alarm(typDCM_TX(intDCMEvent).intUnit, typDCM_TX(intDCMEvent).intSrc, typDCM_TX(intDCMEvent).intDes, 1)
                             
                             'Send Error To Client
                             Call Client_Alarm(typDCM_TX(intDCMEvent).byteHeadEnd, strAlarm)
                             
                             'Check Email
                             If typEmailSettings.blnDCMSpliceGiveUp = True Then
                                Call Email_TX(Chr$(&H1) + Chr$(typEmailSettings.byteList) + "BCI CPS-HS DCM-" + strSplicerCategoryNames(1) + " ERROR-GIVE UP" + "~" + strAlarm + " " + CStr(Now), 0)
                             End If
                            
                             'Clear Event For Use Again
                             typDCM_TX(intDCMEvent).byteMainState = 0
                        Else
                             'Build Alarm
                             strAlarm = "A DCM error has occured. "
                             If lngError = 1000 Then
                                 strAlarm = strAlarm + "  Input IPS_FindTSs Error."
                             End If
                             If lngError = 2000 Then
                                 strAlarm = strAlarm + "  Output IPS_FindTSs Error."
                             End If
                             strAlarm = strAlarm + Client_Build_Alarm(typDCM_TX(intDCMEvent).intUnit, typDCM_TX(intDCMEvent).intSrc, typDCM_TX(intDCMEvent).intDes, 1)
                             
                             'Check Email
                             If typEmailSettings.blnDCMSpliceRetry = True Then
                                Call Email_TX(Chr$(&H1) + Chr$(typEmailSettings.byteList) + "BCI CPS-HS DCM-" + strSplicerCategoryNames(1) + " RETRY" + "~" + strAlarm + " " + CStr(Now), 0)
                             End If
                            
                            'Set To Resend
                             typDCM_TX(intDCMEvent).byteMainState = 1
                        End If
                        
                        
                        'Find Another Splice To Send
                        'Clear
                        blnDCMEvent = False
                        'Check For Any Data To Send
                        For intDCMEvent = 0 To UBound(typDCM_TX)
                            If typDCM_TX(intDCMEvent).intMainRetryLock = 0 Then
                                If typDCM_TX(intDCMEvent).byteMainState = 1 Then
                                    If typDCM_TX(intDCMEvent).intUnit = Index Then
                                        If typDCM_TX(intDCMEvent).intSrc > 0 Then
                                            If typDCM_TX(intDCMEvent).intDes > 0 Then
                                                
                                                'Clear Global Variables
                                                Call DCM_Clear_Globals(Index, intUnit)
                                                
                                                'Build GetGbePortMode
                                                strOut = DCM_GetGbePortMode(intDCMEvent, intUnit, 1)
                                                
                                                'Send
                                                intDCMErrorTimeMain(Index) = 100
                                                wsDCMMain(Index).SendData strOut
                                                typDCM_TX(intDCMEvent).byteMainState = 2
                                                
                                                'Log Data
                                                Call DCM_LOG(Index, intUnit, strOut, True)
                                                
                                                'Set Flag
                                                blnDCMEvent = True
                                                'Exit Loop
                                                Exit For
                                            
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        Next intDCMEvent
                    
                        'Check If Nothing To Send
                        If blnDCMEvent = False Then
                            'Nothing To Send
                            'Logout
                            intDCMErrorTimeMain(Index) = 100
                            strOut = DCM_Login_Logout(Index, intUnit, False)
                            wsDCMMain(Index).SendData strOut
                        
                            'Log It
                            Call DCM_LOG(Index, intUnit, strOut, True)
                        
                        End If
                        
                        'Exit
                        Exit Sub
                    
                    End If
                    
                    
                    'Process Data
                    Select Case (Mid$(strRequestID, 1, 2))
         
                        'Logon
                        Case Chr$(&H0) + Chr$(&H1)
                            'Find A Splice To Send
                            'Clear
                            blnDCMEvent = False
                            'Check For Any Data To Send
                            For intDCMEvent = 0 To UBound(typDCM_TX)
                                If typDCM_TX(intDCMEvent).intMainRetryLock = 0 Then
                                    If typDCM_TX(intDCMEvent).byteMainState = 1 Then
                                        If typDCM_TX(intDCMEvent).intUnit = Index Then
                                            If typDCM_TX(intDCMEvent).intSrc > 0 Then
                                                If typDCM_TX(intDCMEvent).intDes > 0 Then
                                                    
                                                    'Clear Global Variables
                                                    Call DCM_Clear_Globals(Index, intUnit)
                                                    
                                                    'Build GetGbePortMode
                                                    strOut = DCM_GetGbePortMode(intDCMEvent, intUnit, 1)
                
                                                    'Send
                                                    intDCMErrorTimeMain(Index) = 100
                                                    wsDCMMain(Index).SendData strOut
                                                    typDCM_TX(intDCMEvent).byteMainState = 2
                                                    
                                                    'Log Data
                                                    Call DCM_LOG(Index, intUnit, strOut, True)
                                                    
                                                    'Set Flag
                                                    blnDCMEvent = True
                                                    
                                                    'Exit Loop
                                                    Exit For
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            Next intDCMEvent
                        
                            'Check If Nothing To Send
                            If blnDCMEvent = False Then
                                'Nothing To Send
                                'Logout
                                intDCMErrorTimeMain(Index) = 100
                                strOut = DCM_Login_Logout(Index, intUnit, False)
                                wsDCMMain(Index).SendData strOut
                            
                                'Log It
                                Call DCM_LOG(Index, intUnit, strOut, True)
                            End If
        
                        'Logoff
                        Case Chr$(&H0) + Chr$(&H2)
                            'Close Connection
                            intDCMErrorTimeMain(Index) = 100
                            wsDCMMain(Index).Close
        
        
                        'GetGbePortMode
                        Case Chr$(&H1) + Chr$(&H0)
                            'Extract Event #
                            intDCMEvent = Asc(Mid$(strRequestID, 4, 1))
                            
                            'Extract Port Mode
                            dblX = Convert_String_To_Double(Right(strStubData, 4))
                            
                            'Check Results
                            If dblX = 1 Then
                                blnDCMOutputPortBidirectional(Index, intUnit) = False
                            Else
                                blnDCMOutputPortBidirectional(Index, intUnit) = True
                            End If
                            
                            'Request Input IPS_FindTSs
                            strOut = DCM_Input_IPS_FindTSs(intDCMEvent, intUnit, 1)

                            'Send
                            intDCMErrorTimeMain(Index) = 100
                            wsDCMMain(Index).SendData strOut
                            
                            'Log Data
                            Call DCM_LOG(Index, intUnit, strOut, True)
                            
                        'Input IPS_FindTSs
                        Case Chr$(&H2) + Chr$(&H0)
                            'Extract Event #
                            intDCMEvent = Asc(Mid$(strRequestID, 4, 1))
                            
                            'Extract Input Transport Stream ID
                            'Always Pull Transport Stream Using Right(..,4) as There Can Be Extra Padding After UDP Port
                            lngDCMInputTransportIndex(Index, intUnit) = CLng(Convert_String_To_Double(Right(strStubData, 4)))
                            
                            'Check For Error
                            If lngDCMInputTransportIndex(Index, intUnit) = &HFFFFFFFF Then
                                'rror
                                lngError = 1000
                                GoTo 100
                            End If
                            
                            'Request Output IPS_FindTSs
                            strOut = DCM_Output_IPS_FindTSs(intDCMEvent, intUnit, 1)

                            'Send
                            intDCMErrorTimeMain(Index) = 100
                            wsDCMMain(Index).SendData strOut
                            
                            'Log Data
                            Call DCM_LOG(Index, intUnit, strOut, True)
       
                        'Output IPS_FindTSs
                        Case Chr$(&H3) + Chr$(&H0)
                            'Extract Event #
                            intDCMEvent = Asc(Mid$(strRequestID, 4, 1))
                            
                            'Extract Ouput Transport Stream ID
                            'Always Pull Transport Stream Using Right(..,4) as There Can Be Extra Padding After UDP Port
                            lngDCMOutputTransportIndex(Index, intUnit) = CLng(Convert_String_To_Double(Right(strStubData, 4)))
                            
                            'Check For Error
                            If lngDCMOutputTransportIndex(Index, intUnit) = &HFFFFFFFF Then
                                'rror
                                lngError = 2000
                                GoTo 100
                            End If
                            
                            'Request DCM_AlternateSourcesGet
                            strOut = DCM_AlternateSourcesGet(intDCMEvent, intUnit, 1)

                            'Send
                            intDCMErrorTimeMain(Index) = 100
                            wsDCMMain(Index).SendData strOut
                            
                            'Log Data
                            Call DCM_LOG(Index, intUnit, strOut, True)
        
                        'AlternateSourcesGet
                        Case Chr$(&H4) + Chr$(&H0)
                            'Extract Event #
                            intDCMEvent = Asc(Mid$(strRequestID, 4, 1))
        
                            'Get Number Of Alternates
                            lngAltSrc = CLng(Convert_String_To_Double(Mid$(strStubData, 1, 4)))
                            
                            'Check Results
                            If lngAltSrc > 0 Then
                                'Dimension
                                ReDim strAltSrc(lngAltSrc)
                                'Extract Into Array
                                For lngX = 1 To lngAltSrc
                                    'Get Alternates
                                    'Calculate Start
                                    lngStart = ((lngX - 1) * 16) + 5
                                    strAltSrc(lngX) = Mid$(strStubData, lngStart, 16)
                                Next lngX
                                
                                'strAltSrc Data Format
                                'Bytes 1-2 = Input Board
                                'Bytes 3-4 = Input Port
                                'Bytes 5-8 = Input Transport Index
                                'Bytes 9-12 = Input SID (Pgm Number)
                                'Bytes 13-16 = ID# Used For Switch And Delete Commands
                                
                                'Store Base ID# For DeleteAlternate Command
                                strDCMDeleteID(Index, intUnit) = Mid$(strAltSrc(1), 13, 4)
                                
                                'Process Array
                                blnFound = False
                                For lngX = 1 To lngAltSrc
                                    'Check If Requesting Source Already Exists In Array
                                    If Mid$(strAltSrc(lngX), 1, 12) = DCM_Get_Source_Data(intDCMEvent, intUnit, 1) Then
                                        'Exists
                                        blnFound = True
                                        'Switch To It Then
                                        strOut = DCM_SwitchToAlternateSource(intDCMEvent, intUnit, 1, Mid$(strAltSrc(lngX), 13, 4))
                                            
                                        'Send
                                        intDCMErrorTimeMain(Index) = 100
                                        wsDCMMain(Index).SendData strOut
                                        
                                        'Log Data
                                        Call DCM_LOG(Index, intUnit, strOut, True)
                                            
                                        'Exit
                                        Exit For
                                    
                                    End If
                                Next lngX
                                    
                                'Check Not Found
                                If blnFound = False Then
                                    'Did Not Exist So AddAlternateSource
                                    
                                    'Set DCM_AlternateSourcesAdd
                                    strOut = DCM_AlternateSourcesAdd(intDCMEvent, intUnit, 1)
        
                                    'Send
                                    intDCMErrorTimeMain(Index) = 100
                                    wsDCMMain(Index).SendData strOut
                                    
                                    'Log Data
                                    Call DCM_LOG(Index, intUnit, strOut, True)
                                    
                                End If
                            
                            End If
                            
                        'AlternateSourcesAdd
                        Case Chr$(&H5) + Chr$(&H0)
                            'Extract Event #
                            intDCMEvent = Asc(Mid$(strRequestID, 4, 1))
                            
                            'Request DCM_AlternateSourcesGet
                            strOut = DCM_AlternateSourcesGet(intDCMEvent, intUnit, 1)

                            'Send
                            intDCMErrorTimeMain(Index) = 100
                            wsDCMMain(Index).SendData strOut
                            
                            'Log Data
                            Call DCM_LOG(Index, intUnit, strOut, True)
                        
                            
                        'SwitchAlternateSource
                        Case Chr$(&H6) + Chr$(&H0)
                            'Extract Event #
                            intDCMEvent = Asc(Mid$(strRequestID, 4, 1))
                            
                            'Update GUI If Main Winsock
                            'Update TMX Current Source Table
                            Call MotorolaTMXRCV(typDCM_TX(intDCMEvent).intUnit, typDCM_TX(intDCMEvent).intSrc, typDCM_TX(intDCMEvent).intDes)
                    
                            'Clear Event For Use Again
                            typDCM_TX(intDCMEvent).byteMainState = 0

                            'Check If Going Home (Source Service = Destination Service)
                            If typDCM_TX(intDCMEvent).intSrc = typDCM_TX(intDCMEvent).intDes Then
                                'Going Home So Send Delete Alternates
                                 
                                'Send DCM_AlternateSourceDelete
                                strOut = DCM_AlternateSourceDelete(intDCMEvent, intUnit, 1, strDCMDeleteID(Index, intUnit))
    
                                'Send
                                intDCMErrorTimeMain(Index) = 100
                                wsDCMMain(Index).SendData strOut
                                
                                'Log Data
                                Call DCM_LOG(Index, intUnit, strOut, True)
                                 
                            Else
                                'Find Another Splice To Send
                                'Clear
                                blnDCMEvent = False
                                'Check For Any Data To Send
                                For intDCMEvent = 0 To UBound(typDCM_TX)
                                    If typDCM_TX(intDCMEvent).intMainRetryLock = 0 Then
                                        If typDCM_TX(intDCMEvent).byteMainState = 1 Then
                                            If typDCM_TX(intDCMEvent).intUnit = Index Then
                                                If typDCM_TX(intDCMEvent).intSrc > 0 Then
                                                    If typDCM_TX(intDCMEvent).intDes > 0 Then
                                                        
                                                        'Clear Global Variables
                                                        Call DCM_Clear_Globals(Index, intUnit)
                                                        
                                                        'Build GetGbePortMode
                                                        strOut = DCM_GetGbePortMode(intDCMEvent, intUnit, 1)
                                                        
                                                        'Send
                                                        intDCMErrorTimeMain(Index) = 100
                                                        wsDCMMain(Index).SendData strOut
                                                        typDCM_TX(intDCMEvent).byteMainState = 2
                                                        
                                                        'Log Data
                                                        Call DCM_LOG(Index, intUnit, strOut, True)
                                                        
                                                        'Set Flag
                                                        blnDCMEvent = True
                                                        'Exit Loop
                                                        Exit For
                                                    
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                Next intDCMEvent
                            
                                'Check If Nothing To Send
                                If blnDCMEvent = False Then
                                    'Nothing To Send
                                    'Logout
                                    intDCMErrorTimeMain(Index) = 100
                                    strOut = DCM_Login_Logout(Index, intUnit, False)
                                    wsDCMMain(Index).SendData strOut
                                
                                    'Log It
                                    Call DCM_LOG(Index, intUnit, strOut, True)
                                
                                End If
                            End If
                        
                        'AlternateSourceDelete
                        Case Chr$(&H7) + Chr$(&H0)
                            
                            'Find Another Splice To Send
                            'Clear
                            blnDCMEvent = False
                            'Check For Any Data To Send
                            For intDCMEvent = 0 To UBound(typDCM_TX)
                                If typDCM_TX(intDCMEvent).intMainRetryLock = 0 Then
                                    If typDCM_TX(intDCMEvent).byteMainState = 1 Then
                                        If typDCM_TX(intDCMEvent).intUnit = Index Then
                                            If typDCM_TX(intDCMEvent).intSrc > 0 Then
                                                If typDCM_TX(intDCMEvent).intDes > 0 Then
                                                    
                                                    'Clear Global Variables
                                                    Call DCM_Clear_Globals(Index, intUnit)
                                                    
                                                    'Build GetGbePortMode
                                                    strOut = DCM_GetGbePortMode(intDCMEvent, intUnit, 1)
                                                    
                                                    'Send
                                                    intDCMErrorTimeMain(Index) = 100
                                                    wsDCMMain(Index).SendData strOut
                                                    typDCM_TX(intDCMEvent).byteMainState = 2
                                                    
                                                    'Log Data
                                                    Call DCM_LOG(Index, intUnit, strOut, True)
                                                    
                                                    'Set Flag
                                                    blnDCMEvent = True
                                                    'Exit Loop
                                                    Exit For
                                                
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            Next intDCMEvent
                        
                            'Check If Nothing To Send
                            If blnDCMEvent = False Then
                                'Nothing To Send
                                'Logout
                                intDCMErrorTimeMain(Index) = 100
                                strOut = DCM_Login_Logout(Index, intUnit, False)
                                wsDCMMain(Index).SendData strOut
                            
                                'Log It
                                Call DCM_LOG(Index, intUnit, strOut, True)
                            
                            End If
        
        
                    End Select
                End If
            End If
        End If
        
        
End Sub
